package ip.ejb.entity;
/***********************************************************************
 * Module:  OdsekBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class OdsekBean
 ***********************************************************************/

import ip.ejb.sequence.*;

import java.util.*;
import javax.ejb.*;
import javax.naming.InitialContext;

/**
 * @ejb.bean 
 * 	cmp-version="2.x"
 *  name="OrderItem"
 *  type="CMP"
 *  jndi-name="ip/ejb/entity/OrderItem"
 *  local-jndi-name="ip/ejb/entity/OrderItemLocal"
 *  reentrant="false"
 *  
 *  
 * @ejb.persistence
 * 	table-name="orderitem"
 * 
 * @ejb.transaction
 * 	type="Required"
 * 
 * @jboss.table-name
 * 	table-name="orderitem"
 * 
 * @jboss.create-table
 * 	create="true"
 * 
 * @jboss.remove-table
 * 	remove="false"
 */
public abstract class OrderItemBean implements javax.ejb.EntityBean
{
//	Da li je ovo uopste moguce???? 
//	* @ejb.finder      
//	 *  signature="java.util.Collection findByOrderId(java.lang.String orderId)"
//	 *  query="SELECT OBJECT(oi) FROM OrderItem oi WHERE oi.order_id = ?1"
//	 *  method-intf="LocalHome" 
//	The problem with this solution is that this will create a field in the target EJB, database table named BranchID. The target EJB cannot be defined with getter, setter, finder or selector functions for BranchID. Creating an extra BranchID in the target EJB will create the error:
//
//		org.jboss.ejb.plugins.cmp.ejbql.UnknownPathException: Unknown terminal field:


	private EntityContext ctx = null;
	private SequenceSessionLocal sequenceSession = null;

	/**
	 * @ejb.create-method view-type="both"
	 */
	public OrderItemPK ejbCreate(int quantity, MovieLocal movie, OrderLocal order) throws CreateException {

		setOrderItemId(sequenceSession.getNextValue("OrderItem"));
		setQuantity(quantity);

		return null;
	}  
	
	public void ejbPostCreate(int quantity, MovieLocal movie, OrderLocal order){
		setMovie(movie);
		setOrder(order);
	}
	
	public void setEntityContext(EntityContext ctx) throws javax.ejb.EJBException
	{
		SequenceSessionLocalHome home = null;
		Object obj = null;
		this.ctx = ctx;
		try {
			obj = (new InitialContext()).lookup("ejb/util/SequenceSessionLocal");
			home = (SequenceSessionLocalHome) obj;
			sequenceSession = home.create();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void unsetEntityContext() throws javax.ejb.EJBException
	{
		this.ctx = null;
		try {
			sequenceSession.remove();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * @ejb.persistence column-name="orderitem_id"
	 * @ejb.interface-method view-type="both"
	 * @ejb.pk-field
	 * @jboss.jdbc-type type="INTEGER"
	 * @jboss.sql-type  type="INTEGER"
	 */
	public abstract int getOrderItemId();
	
	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrderItemId(int orderItemId);

	/**
	 * @ejb.persistence column-name="quantity"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="INTEGER"
	 * @jboss.sql-type  type="INTEGER"
	 */
	public abstract int getQuantity();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setQuantity(int quantity);
	
	/**
	 * @ejb.interface-method view-type="local"
	 * @ejb.relation
	 *   name="OrderItem-Order"
	 *   role-name="Entire-Order"
	 *   cascade-delete="no"
	 * @jboss.relation
	 *   fk-constraint="true"
	 *   fk-column="orderId"
	 *   related-pk-field="orderId"
	 */
	public abstract OrderLocal getOrder();
	
	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrder(OrderLocal order);

	/**
	 * @ejb.interface-method view-type="local"
	 * @ejb.relation
	 *   name="Movie-OrderItem"
	 *   role-name="OrderItem-like-Movie"
	 *   cascade-delete="yes"
	 * @jboss.relation
	 *   fk-constraint="true"
	 *   fk-column="movieId"
	 *   related-pk-field="movieId"
	 */
	public abstract MovieLocal getMovie();
	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setMovie(MovieLocal movie);
	
	public void ejbActivate() throws javax.ejb.EJBException	{}
	public void ejbLoad() throws javax.ejb.EJBException{}
	public void ejbPassivate() throws javax.ejb.EJBException{}
	public void ejbRemove() throws javax.ejb.RemoveException, javax.ejb.EJBException{}
	public void ejbStore() throws javax.ejb.EJBException{}

}